using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows.Forms;

namespace GestionLabo.Presentation
{
    public class NumericTextBox : TextBox
    {
        bool allowSpace = false;

        // Restricts the entry of characters to digits (including hex), the negative sign,
        // the decimal point, and editing keystrokes (backspace).

        protected override void OnValidating(CancelEventArgs e)
        {
            base.OnValidating(e);
            //if(base.Text.
            base.Text = this.Text.ToString(CultureInfo.CurrentCulture.NumberFormat); 
        }
        //protected void OnValueChanged(EventArgs eventargs)
        //{
        //    throw new Exception("The method or operation is not implemented.");
        //}

        


        protected override void OnKeyUp(KeyEventArgs e)
        {
            base.OnKeyUp(e);
            decimal d = 0;
            decimal.TryParse(this.Text, out d);
            base.Text = d.ToString(CultureInfo.CurrentCulture.NumberFormat);
            this.DecimalValue = d;
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);

            NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
            string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
            string groupSeparator = numberFormatInfo.NumberGroupSeparator;
            string negativeSign = numberFormatInfo.NegativeSign;

            string keyInput = e.KeyChar.ToString();

            if (Char.IsDigit(e.KeyChar))
            {
                base.Text = this.Text;
            }
            else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||
             keyInput.Equals(negativeSign))
            {
                // Decimal separator is OK
            }
            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK
            }
            else if (e.KeyChar == '.')
            {
                // Backspace key is OK
            }
            //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)
            //    {
            //     // Let the edit control handle control and alt key combinations
            //    }
            else if (this.allowSpace && e.KeyChar == ' ')
            {

            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
        }

        public int IntValue
        {
            get
            {
                return Int32.Parse(this.Text);
            }
        }

        public decimal DecimalValue
        {
            get
            {
                if (this.Text== string.Empty)
                    this.Text = "0";
                return Decimal.Parse(this.Text);
            }
            set
            {
                value = Convert.ToDecimal(string.Format("{0:0.000}", value), CultureInfo.CurrentCulture.NumberFormat);
            }
        }

        public bool AllowSpace
        {
            set
            {
                this.allowSpace = value;
            }

            get
            {
                return this.allowSpace;
            }
        }

        
    }
}
